<!--
 * @Author: your name
 * @Date: 2021-12-07 10:27:01
 * @LastEditTime: 2021-12-07 11:30:23
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \toutiao-m\src\views\User\components\UserEditBirthday.vue
-->

<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="confirm"
    />
  </div>
</template>

<script>
import dayJs from 'dayjs'
import { UpdateUserProfile } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      currentDate: null,
      minDate: new Date(1600, 1, 1),
      maxDate: new Date()
    }
  },

  created () {
    this.currentDate = new Date(this.value)
  },

  methods: {
    async confirm (value) {
      Toast.loading({
        message: '更新中...',
        duration: 0,
        forbidClick: true
      })
      const formatTime = dayJs(value).format('YYYY-MM-DD')
      try {
        await UpdateUserProfile({
          birthday: formatTime
        })
        this.$emit('close')
        this.$emit('input', formatTime)
        Toast.success('更新成功！！')
      } catch (error) {
        Toast.fail('更新失败！！！')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
